package com.tbit.uqbike.webmanager.controller;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.StrUtil;
import com.tbit.common.entity.pojo.dto.AddFaultDTO;
import com.tbit.common.entity.view.Result;
import com.tbit.maintain.api.AddFaultApi;
import com.tbit.uqbike.object.pojo.MachineFault;
import com.tbit.uqbike.object.value.LoginInfo;
import com.tbit.uqbike.object.view.JsonResult;
import com.tbit.uqbike.service.rpcservice.MachineFaultService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;
import javax.validation.constraints.NotNull;
import java.util.Objects;

/**
 * 故障管理
 *
 * @author Leon
 * 2019年11月16日 下午3:11:00
 */
@Validated
@RestController
@RequestMapping("/machineFault")
public class MachineFaultController extends BaseController {
    @Autowired
    private MachineFaultService machineFaultService;
    @Autowired
    private AddFaultApi addFaultApi;

    /* 根据设备编号查询*/
    @RequestMapping("/getByMachineId")
    public JsonResult getByMachineId(@NotNull(message = "ver.machineId.notNull") Integer machineId, Integer mapType, HttpServletRequest request) {
        LoginInfo info = getLogin(request);
        MachineFault machineFault = machineFaultService.getByMachineId(info, machineId, mapType);
        return JsonResult.ok(machineFault);
    }


    /**
     * 添加故障
     * @param machineFault  故障信息
     * @param request   request
     * @return Result
     */
    @PostMapping("/addMachineFault")
    public Result<Object> addMachineFault(@RequestBody @Valid MachineFault machineFault, HttpServletRequest request) {
        LoginInfo info = getLogin(request);
        AddFaultDTO addFaultDTO = new AddFaultDTO();
        BeanUtil.copyProperties(machineFault, addFaultDTO);
        addFaultDTO.setAccountUserId(info.getAccountUserId())
                .setPhone(info.getPhone())
                .setName(info.getName());
        return addFaultApi.addFault(addFaultDTO);
    }

}